var gulp = require('gulp');
var path = require('path');
var sourcemaps = require('gulp-sourcemaps');
var runSequence = require('run-sequence');
var browserSync = require('browser-sync');
var uglify = require('gulp-uglify');
var tslint = require("gulp-tslint");
var concat = require('gulp-concat');
var reload = browserSync.reload;
var $ = require('gulp-load-plugins')();
var ts = require('gulp-typescript');
var del = require('del');

gulp.task('clean', function () {
  return del('dist')
});
const jsNPMDependencies = [
  'core-js/client/shim.min.js',
  'zone.js/dist/zone.js',
  'reflect-metadata/Reflect.js',
  'systemjs/dist/system.src.js',
];
const angularJs = [
  '@angular/core/bundles/core.umd.js',
  '@angular/common/bundles/common.umd.js',
  '@angular/compiler/bundles/compiler.umd.js',
  '@angular/platform-browser/bundles/platform-browser.umd.js',
  '@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
  '@angular/http/bundles/http.umd.js',
  '@angular/router/bundles/router.umd.js',
  '@angular/forms/bundles/forms.umd.js',
  '@angular/upgrade/bundles/upgrade.umd.js',
  'rxjs/*.js',
  'rxjs/**/*.js',
  'angular-in-memory-web-api/*.js',
  'angular-in-memory-web-api/*/*.js',
  'traceur/bin/*.js',
]
gulp.task('copy', function () {
  var mappedPaths = jsNPMDependencies.map(file => {
    return path.resolve('node_modules', file)
  })
  gulp.src(mappedPaths, {base: 'node_modules'})
    .pipe(concat('bundles.js'))
    .pipe(gulp.dest('dist/lib'))
  var angularJsPaths = angularJs.map(file => {
    return path.resolve('node_modules', file)
  })
  gulp.src(angularJsPaths, {base: 'node_modules'})
    .pipe(gulp.dest('dist/lib'))
});

gulp.task('views', function () {
  gulp.src(['src/*.*'])
    .pipe(gulp.dest('dist'))
  gulp.src('src/app/*/views/*')
    .pipe(gulp.dest('dist/app/'))
});

gulp.task('ts:dev', function () {
  var tsProject = ts.createProject('src/tsconfig.json');
  var tsResult = gulp.src(['src/*.ts','src/**/*.ts'])
    .pipe(sourcemaps.init())
    .pipe(tsProject(tsResult))
  return tsResult.js
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('dist'))
});

gulp.task('ts:prod', function () {
  var tsProject = ts.createProject('src/tsconfig.json');
  var tsResult = gulp.src(['src/*.ts','src/**/*.ts'])
    .pipe(sourcemaps.init())
    .pipe(tsProject(tsResult))
  return tsResult.js
    .pipe(sourcemaps.write())
    .pipe(uglify())//压缩
    .pipe(gulp.dest('dist'))
});

gulp.task('browser-sync', function () {
   var files = [
      'src/*.html',
      'src/*.ts',
      'src/app/*.ts',
      'src/app/**/*.ts',
      'src/app/**/views/*'
   ];

   browserSync.init(files, {
      server: {
         baseDir: './dist'
      }
   });
});

gulp.task('lint', function() {  
  return gulp.src([
      'src/*.ts',
      'src/app/*.ts',
      'src/app/**/*.ts'])
    .pipe(tslint({
      formatter: "verbose"
    }))
    .pipe(tslint.report())
});

gulp.task('build', function (callback) {
  runSequence('clean', 'lint', 'views', 'copy', 'ts:prod', callback);
});

gulp.task('dev', function () {
  runSequence('clean', 'lint', 'views', 'copy', 'ts:dev', 'browser-sync', function(){
    gulp.watch([
      'src/*.ts',
      'src/app/*.ts',
      'src/app/**/*.ts',
    ], ['ts:dev', 'lint']).on('change', reload);
    gulp.watch([
      'src/*.html',
      'src/app/**/views/*'
    ], ['views', 'lint']).on('change', reload);
  });
});

gulp.task('default', ['dev']);